<script setup lang="ts">
import { ref } from 'vue'
import type {
  KnowledgeType,
  KnowledgeParams,
  KnowledgeList
} from '@/types/consult'
import KnowledgeCard from './KnowledgeCard.vue'
import { getKnowledgeListAPI } from '@/services/consults'

const list = ref<KnowledgeList>([])
const loading = ref(false)
const finished = ref(false)

const props = defineProps<{
  type: KnowledgeType
}>()

// 定义查询参数
const queryParams: KnowledgeParams = {
  type: props.type,
  current: 1,
  pageSize: 5
}
const onLoad = async () => {
  // 异步更新数据
  // 1.调用接口获取数据
  const res = await getKnowledgeListAPI(queryParams)
  // 2.把数据追加到数据列表中
  // list.value = res.data.rows
  list.value.push(...res.data.rows)
  // 3.设置loading为false
  loading.value = false
  // 4.判断所有数据是否加载完毕
  if (queryParams.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    queryParams.current++
  }
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card
        :Knowledge="item"
        v-for="item in list"
        :key="item.id"
      ></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
